package com.shenyue.nbtownlet.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.hitomi.glideloader.GlideImageLoader;
import com.hitomi.tilibrary.style.progress.ProgressPieIndicator;
import com.hitomi.tilibrary.transfer.TransferConfig;
import com.hitomi.tilibrary.transfer.Transferee;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivitySpecialMeasuresDetailBinding;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.model.AppDissertationModel;
import com.shenyue.nbtownlet.model.DefaultModel;
import com.shenyue.nbtownlet.util.DensityUtil;
import com.shenyue.nbtownlet.util.GlideUtil;
import com.shenyue.nbtownlet.util.StringUtil;
import com.trycatch.mysnackbar.Prompt;
import com.trycatch.mysnackbar.TSnackbar;

import org.sufficientlysecure.htmltextview.HtmlResImageGetter;

import java.util.ArrayList;
import java.util.List;

/**
 * 专项整治详情
 */

public class SpecialMeasuresDetailActivity extends BaseActivity implements BaseListener {
    private ActivitySpecialMeasuresDetailBinding binding;
    private Transferee transferee;
    private Context mContext;
    private Handler mHandler = new Handler();
    private int[] wh;
    private List<ImageView> mList = new ArrayList<>();
    private List<String> mImages = new ArrayList<>();
    private TSnackbar snackBar;
    private JSONObject dataSource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_special_measures_detail);
        Bundle bundle = getIntent().getExtras();
        dataSource = JSON.parseObject(bundle.getString(DefaultModel.PAGE_DATA));
        setActionBar(true, dataSource.getString("NAME"));
        mContext = this;
        transferee = Transferee.getDefault(mContext);

        wh = DensityUtil.getWH(this, 2, 24);

        snackBar = showTSnackbar("正在查询数据..., 请稍候");
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                findDetail();
            }
        }, 32);
    }

    private void findDetail() {
        JSONObject param = new JSONObject();
        param.put("id", dataSource.getString("ID"));
        AppDissertationModel.GetPointById(mContext, param, this);
    }

    private void initView(JSONObject data) {
        binding.NAME.setText(StringUtil.getValue(data.getString("NAME")));
        binding.ADDRESS.setText(StringUtil.getValue(data.getString("ADDRESS")));
        binding.PIDNAME.setText(StringUtil.getValue(data.getString("PIDNAME")));
        binding.DESCRIPTION.setHtml(StringUtil.getValue(data.getString("DESCRIPTION")),
                new HtmlResImageGetter(binding.DESCRIPTION));

        String PHOTOURL = StringUtil.getValue(data.getString("PHOTOURL"));
        if (StringUtil.isNull(PHOTOURL))
            return;
        String[] array = PHOTOURL.split(",");
        for (int i = 0, j = array.length; i < j; i++) {
            mImages.add(array[i]);
        }

        binding.PHOTOURL.removeAllViews();
        for (int i = 0; i < mImages.size(); i++) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(wh[0], wh[1]);
            ImageView imageView = new ImageView(mContext);
            imageView.setId(i);
            imageView.setLayoutParams(layoutParams);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TransferConfig config = TransferConfig.build()
                            .setNowThumbnailIndex(v.getId())
                            .setSourceImageList(mImages)
                            .setMissPlaceHolder(R.mipmap.ic_empty_photo)
                            .setOriginImageList(mList)
                            .setProgressIndicator(new ProgressPieIndicator())
                            .setImageLoader(GlideImageLoader.with(mContext.getApplicationContext()))
                            .create();
                    transferee.apply(config).show(new Transferee.OnTransfereeStateChangeListener() {
                        @Override
                        public void onShow() {
                            Glide.with(mContext).pauseRequests();
                        }

                        @Override
                        public void onDismiss() {
                            Glide.with(mContext).resumeRequests();
                        }
                    });
                }
            });
            binding.PHOTOURL.addView(imageView);
            mList.add(imageView);
            GlideUtil.loadImgCenterCrop(mContext, mImages.get(i), imageView);
        }
    }


    @Override
    public void onDestroy() {
        if (transferee != null)
            transferee.destroy();
        super.onDestroy();
    }

    @Override
    public void success(String val, String action) {
        if (AppDissertationModel.XCZTHEMATICDATA_GETPOINTBYID_ACTION.equals(action)) {
            hideTSnackbar(snackBar, "数据查询成功");
            JSONObject $data = JSON.parseObject(val);
            JSONObject Data = $data.getJSONObject("Data");
            initView(Data);
        }
    }

    @Override
    public void error(String error, String action) {
        hideTSnackbar(snackBar, error, Prompt.WARNING);
    }
}
